use std::ffi::c_int;

#[repr(C)]
#[derive(Clone, Copy, Default)]
pub struct CBoolean(pub c_int);

impl Into<bool> for CBoolean {
    fn into(self) -> bool {
        self.0 != 0
    }
}

impl From<bool> for CBoolean {
    fn from(value: bool) -> Self {
        Self(if value { 1 } else { 0 })
    }
}

mod test {
    #[test]
    fn test_c_boolean() {
        use crate::{State, bindings::ThreadStatus};
        
        let state = State::new();
        assert!(state.do_string("a = true\nb = false\nc = 1\nd = 3.141") == ThreadStatus::Ok);
        state.get_global("a");
        state.get_global("b");
        assert!(state.to_bool(-1) == false);
        assert!(state.to_bool(-2) == true);
        state.get_global("c");
        let mut is_num = false;
        state.to_integerx(-1, Some(&mut is_num));
        assert!(is_num);
        state.to_numberx(-1, Some(&mut is_num));
        assert!(is_num);
        state.get_global("d");
        state.to_integerx(-1, Some(&mut is_num));
        assert!(!is_num);
        state.to_numberx(-1, Some(&mut is_num));
        assert!(is_num);
    }
}